package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mapper.BlackListMapper;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domain.BlackList;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.MyPageResultVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class BlackListApiImpl implements BlackListApi{

    @Autowired
    private BlackListMapper blackListMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 黑名单分页查询(自己实现思路)
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public Page blackList(Long userId, int page, int pagesize) {
        Page<UserInfo> userInfoPage = new Page<>(page,pagesize);

        LambdaQueryWrapper<BlackList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlackList::getUserId,userId);

        List<BlackList> blackLists = blackListMapper.selectList(queryWrapper);

        List<Long> userIds = blackLists.stream().map((item) -> {
            Long blackUserId = item.getBlackUserId();

            return blackUserId;
        }).collect(Collectors.toList());

        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();

        userInfoLambdaQueryWrapper.in(UserInfo::getId,userIds);
        userInfoMapper.selectPage(userInfoPage,userInfoLambdaQueryWrapper);

        return userInfoPage;
    }

    /**
     * 移除黑名单
     *
     * @param uid
     */
    @Override
    public void removeBlackUser(Long uid,Long userId) {
        blackListMapper.delete(Wrappers.<BlackList>lambdaQuery().eq(BlackList::getUserId,userId)
                .eq(BlackList::getBlackUserId,uid));
    }

    /**
     * 黑名单分页查询(课本实现思路)
     *
     * @param page
     * @param pagesize
     * @param userId
     * @return
     */
    @Override
    public IPage<UserInfo> blackList2(int page, int pagesize, Long userId) {

        Page blackPage = new Page(page,pagesize);

        //自定义sql语句
        return userInfoMapper.findByBlackList(blackPage,userId);
    }
}
